﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComputationalGeometry.Algorithms;
using ComputationalGeometry.Shapes;
using NUnit.Framework;

namespace ComputationalGeometry.UnitTests
{
    [TestFixture]
    public class ConvexHull2D
    {
        [Test]
        public void GetConvexHull2DTest()
        {
            List<Point2D> vertices = new List<Point2D>
                                         {
                                             new Point2D(1, 3),
                                             new Point2D(1, 5),
                                             new Point2D(2, 6),
                                             new Point2D(3, 5),
                                             new Point2D(4, 5),
                                             new Point2D(5, 4),
                                             new Point2D(4, 2),
                                             new Point2D(3, 1),
                                             new Point2D(2, 1),
                                             new Point2D(2, 4),
                                             new Point2D(3, 3)
                                         };

            List<Point2D> expected = new List<Point2D>
                                         {
                                             new Point2D(1, 3),
                                             new Point2D(1, 5),
                                             new Point2D(2, 6),
                                             new Point2D(4, 5),
                                             new Point2D(5, 4),
                                             new Point2D(4, 2),
                                             new Point2D(3, 1),
                                             new Point2D(2, 1)
                                         };

            List<Point2D> convexHullVertices = ConvexHull.GetConvexHull2D(vertices);

            // sort the list by x-coordinate and then by y-coordinates
            Point2DComparer vComparer = new Point2DComparer
                                            {
                                                ComparisonType = PointComparisonType.CompareXThenY
                                            };

            expected.Sort(vComparer.Compare);
            convexHullVertices.Sort(vComparer.Compare);
            Assert.AreEqual(expected.Count, convexHullVertices.Count);

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.IsTrue(
                    convexHullVertices[i] == expected[i],
                    string.Format("Convex Hull 2D did not have point: {0}", expected[i]));
            }
        }
    }
}
